package com.jilijili.work;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.jilijili.controller.BaseInfoProperties;
import com.jilijili.pojo.Email;
import com.jilijili.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @BelongsProject: jilijili-red-book-dev
 * @BelongsPackage: com.jilijili.work
 * @Author: ljj
 * @CreateTime: 2022-04-18  01:28
 * @Description: TODO 定时发送邮件
 * @Version: 1.0
 */
@Slf4j
@Component
public class SendEmailsRegularly extends BaseInfoProperties {



    @Resource
    private EmailService emailService;

    @Autowired
    private ServiceModel model;


    //每天早上6点开始发送邮件,每次时隔一小时
    @Scheduled(cron = "0 0 8,18 * * ?")
    public void execute() {
        List<ServiceInstance> serviceInstance = this.getServiceInstance();

        StringBuilder text = new StringBuilder();
        for (ServiceInstance instance : serviceInstance) {
            text.append("服务名称:").append(instance.getServiceId()).append("\n")
                    .append("服务器地址:").append(instance.getUri()).append("\n")
                    .append("数据信息:").append(instance.getMetadata()).append("\n")
                    .append("配置数据:").append(model.toString()).append("\n");
        }

        Email build = Email.builder()
                .to("1918652173@qq.com")
                .subject("JiLiEmail")
                .text(text.toString()).build();
        log.info(text.toString());
        this.emailService.sendSimpleMail(build);
    }

}
